package fairygui.editor.publish.gencode
{
   import fairygui.editor.publish.PublishStep;
   import fairygui.editor.settings.GlobalPublishSettings;
   import fairygui.editor.utils.PinYinUtil;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.UtilsStr;
   import flash.filesystem.File;
   
   public class GenerateCodeBase extends PublishStep
   {
       
      
      protected var settings:GlobalPublishSettings;
      
      protected var thisPackageFolder:File;
      
      protected var thisPackageName:String = "";
      
      protected var thisPackagePath:String = "";
      
      protected var sortedClasses:Array;
      
      protected var classInfo:Object;
      
      public function GenerateCodeBase()
      {
         this.sortedClasses = [];
         super();
      }
      
      protected function init(param1:String) : void
      {
         var path:String = null;
         var targetFolder:File = null;
         var oldFiles:Array = null;
         var file:File = null;
         var fileContent:String = null;
         var fileExtName:String = param1;
         this.settings = publishData._project.settingsCenter.publish;
         try
         {
            path = this.settings.codePath;
            path = UtilsStr.formatStringByName(path,publishData._project.customProperties);
            targetFolder = new File(publishData.project.basePath).resolvePath(path);
            if(!targetFolder.exists)
            {
               targetFolder.createDirectory();
            }
            else if(!targetFolder.isDirectory)
            {
               stepCallback.addMsg("Invalid code path!");
               stepCallback.callOnFail();
               return;
            }
         }
         catch(err:Error)
         {
            stepCallback.addMsg("Invalid code path!");
            stepCallback.callOnFail();
            return;
         }
         this.thisPackageName = PinYinUtil.toPinyin(publishData.targetUIPackage.name,false,false,false);
         this.thisPackageFolder = new File(targetFolder.nativePath + File.separator + this.thisPackageName);
         if(!this.settings.packageName || this.settings.packageName.length == 0)
         {
            this.thisPackagePath = this.thisPackageName;
         }
         else
         {
            this.thisPackagePath = this.settings.packageName + "." + this.thisPackageName;
         }
         if(this.thisPackageFolder.exists)
         {
            oldFiles = this.thisPackageFolder.getDirectoryListing();
            for each(file in oldFiles)
            {
               if(!(file.isDirectory || file.extension != fileExtName))
               {
                  fileContent = UtilsFile.loadString(file);
                  if(UtilsStr.startsWith(fileContent,GenCodeUtils.FILE_MARK))
                  {
                     UtilsFile.deleteFile(file);
                  }
               }
            }
         }
         else
         {
            this.thisPackageFolder.createDirectory();
         }
         GenCodeUtils.prepare(publishData);
         for each(this.classInfo in publishData.outputClasses)
         {
            if(!this.classInfo.ignored)
            {
               this.sortedClasses.push(this.classInfo);
            }
         }
         this.sortedClasses.sortOn("classId");
      }
      
      protected function loadTemplate(param1:String) : void
      {
         var _loc3_:Object = null;
         var _loc2_:File = new File(publishData.project.basePath + "/template/" + param1);
         if(_loc2_.exists)
         {
            _loc3_ = this.loadTemplate2(_loc2_);
            if(_loc3_["Binder"] && _loc3_["Component"])
            {
               this.createFile(_loc3_);
               return;
            }
         }
         _loc2_ = File.applicationDirectory.resolvePath("template/" + param1);
         _loc3_ = this.loadTemplate2(_loc2_);
         this.createFile(_loc3_);
      }
      
      private function loadTemplate2(param1:File) : Object
      {
         var _loc4_:File = null;
         var _loc5_:String = null;
         var _loc2_:Array = param1.getDirectoryListing();
         var _loc3_:Object = {};
         for each(_loc4_ in _loc2_)
         {
            if(_loc4_.extension == "template")
            {
               _loc5_ = _loc4_.name.replace(".template","");
               _loc3_[_loc5_] = UtilsFile.loadString(_loc4_);
            }
         }
         return _loc3_;
      }
      
      protected function createFile(param1:*) : void
      {
         throw new Error("子类需实例化此方法GenerateCodeBase:createFile");
      }
   }
}
